package com.aqs;

public class Test {

    private static MysLockAQS mysLockAQS = new MysLockAQS(true);
    public static void main(String[] args) throws InterruptedException {
        mysLockAQS.lock();
        System.out.println("主线程加锁: " + Thread.currentThread().getName());
        for(int i = 0; i < 10; i++){
            // 开启10条子线程，进行公平锁与非公平锁校验
            new Thread(()->{
                System.out.println("start: " + Thread.currentThread().getName());
                mysLockAQS.lock();
                System.out.println("end: " + Thread.currentThread().getName());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mysLockAQS.unLock();
            }).start();
        }

//        System.out.println("主线程解锁: " + Thread.currentThread().getName());
        test2();
        mysLockAQS.unLock();

    }

    public static void test2() throws InterruptedException {

        mysLockAQS.lock();
        System.out.println("主线程2次加锁: " + Thread.currentThread().getName());
        Thread.sleep(200);

        test3();
        mysLockAQS.unLock();
    }


    public static void test3() throws InterruptedException {

        mysLockAQS.lock();
        System.out.println("主线程3次加锁: " + Thread.currentThread().getName());
        Thread.sleep(300);
        mysLockAQS.unLock();
    }

}
